package com.findu.fragment;

import java.util.ArrayList;
import java.util.HashMap;

import com.example.findu.R;
import com.findu.activity.Activity_publish_Dome;
import com.findu.control.Home_dynamic_XListView;
import com.findu.control.Public_title;
import com.findu.control.Home_dynamic_XListView.IXListViewListener;

import android.os.Bundle;
import android.os.Handler;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.SimpleAdapter;;

public class Fragment_Conversaction_dynamic_4 extends Fragment implements IXListViewListener{
	private View fragmentView;
	
	Public_title public_title;	//标题控制
	// 声明布局文件
	private View include_view;
	
	//声明标题右边图片
	private ImageView title_RightImageView;
	
	//处理列表声明
	private Home_dynamic_XListView mListView;
	private SimpleAdapter mAdapter1;
	private Handler mHandler;
	private ArrayList<HashMap<String, Object>> dlist;
	
	/** 初始化本地数据 */
	String data[] = new String[] { "周氏" ,"王氏"};
	String data1[] = new String[] { "好想念那一季的时光，想念那齿唇间的温度，想念那透过心房的话调。那一季，月光羽化了岁月的风景，那一季，时光撼动了人生的美丽。" 
			,"世界那么大，我想去看看美景旅游旅行......"
	};
	
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		// 引入并得到view
		fragmentView = inflater.inflate(R.layout.fragment__conversaction_dynamic_4, container,false);
		
		
		// 初始化布局文件
		include_view = fragmentView.findViewById(R.id.fragment_conversation_page4_include);
		public_title = new Public_title(include_view);
		public_title.setRighttImage(R.drawable.add);
		public_title.setTextView("动态");
		
		
		title_RightImageView = (ImageView) fragmentView.findViewById(R.id.public_title_rightImage);
		title_RightImageView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent intent = new Intent();
				intent.setClass(getActivity(), new Activity_publish_Dome().getClass());
				startActivity(intent);
			}
		});
		
		
		//---------------------列表部分
		/** 下拉刷新，上拉加载 */
		dlist = new ArrayList<HashMap<String, Object>>();
		mListView = (Home_dynamic_XListView) fragmentView.findViewById(R.id.techan_xListView);// 这个listview是在这个layout里面
		mListView.setPullLoadEnable(true);// 设置让它上拉，FALSE为不让上拉，便不加载更多数据
		mAdapter1 = new SimpleAdapter(getActivity(), getData(),
				R.layout.fragment_dynamic_list_items, new String[] { "name", "img", "content" },
				new int[] { R.id.fragment_Dynamic_Name, R.id.fragment_Dynamic_ImageView, R.id.fragment_Dynamic_Context });
		mListView.setAdapter(mAdapter1);
		mListView.setXListViewListener(this);
		mHandler = new Handler();
		
		
		return fragmentView;
	}
	
	private ArrayList<HashMap<String, Object>> getData() {
		for (int i = 0; i < data.length; i++) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("name", data[i]);
			map.put("content", data1[i]);
			map.put("img", R.drawable.bg_2x);
			dlist.add(map);
		}
		return dlist;
		
	}
	/** 停止刷新， */
	private void onLoad() {
		mListView.stopRefresh();
		mListView.stopLoadMore();
		mListView.setRefreshTime("刚刚");
	}
	// 刷新
		@Override
		public void onRefresh() {
			mHandler.postDelayed(new Runnable() {

				@Override
				public void run() {
					getData();
					mListView.setAdapter(mAdapter1);
					onLoad();
				}
			}, 2000);
		}
		
		// 加载更多
		@Override
		public void onLoadMore() {
			mHandler.postDelayed(new Runnable() {

				@Override
				public void run() {
					getData();
					mAdapter1.notifyDataSetChanged();
					onLoad();
				}
			}, 2000);
		}
		
		
		


	}

